<?php
/**
 * 获取一个模板的编译路径 绝对路径
 *
 * @param string $tplname
 * @return string
 */
function Tpl($tplname){
	global $very;
	if (empty($tplname))
	{
		defined('SCR') ? $tplname = SCR : throwError('没有定义模板变量');
	}
	$tplpath = 'template/'; //模板的路径
	$default_tplpath = $tplpath . 'default/' . $tplname . '.htm';
	if (empty($very['tpl']))
	{
		return temp_cache($default_tplpath);
	}
	else 
	{
		$tplpath .= $very['tpl'] . '/' . $tplname . '.htm';
		if (!file_exists(D_P . $tplpath)) $tplpath = $default_tplpath;
		return temp_cache($tplpath);
	}
	/*
	
	global $cid,$user_tplpath,$default_tplpath,$catedb, $very;
	if(!$catedb) require_once(D_P.'data/cache/cate.php');
	if($tplname==''){
		switch (SCR){
			case 'list':
				if($catedb[$cid]['tpl_index']){
					$tplname = $user_tplpath.'/'.$catedb[$cid]['tpl_index'];
				}else{
					$tplname = $default_tplpath.'/list.htm';
				}
				break;
			case 'view':
				if($catedb[$cid]['tpl_content']){
					$tplname = $user_tplpath.'/'.$catedb[$cid]['tpl_content'];
				}else {
					$tplname = $default_tplpath.'/content.htm';
				}
				break;
			default:
				$tplname = $default_tplpath.'/'.strtolower(SCR).'.htm';
				break;
		}
	}elseif (strpos($tplname,'.')===false){
		$tplname = $default_tplpath.'/'.$tplname.'.htm';
	}else{
		$tplname = $user_tplpath.'/'.$tplname;
	}
	$tplname = temp_cache($tplname);
	return D_P.$tplname;
	*/
}

/**
 * 编译模板文件并返回编译过的路径
 *
 * @param string $tplname
 * @return string
 */
function temp_cache($tplname){
	!file_exists(R_P.$tplname) && throwError($tplname);
	$file_str	= '';
	$cache_name = str_replace('/','_',$tplname);
	$cache_name = substr($cache_name,strlen('template/'));
	$cache_name = 'data/tpl_cache/'.$cache_name;

	if(!file_exists(D_P.$cache_name) || filemtime(R_P.$tplname)>filemtime(D_P.$cache_name)){
		$file_str = readover(R_P.$tplname);
		$parse=1;
		if($parse){
			$s = array(
			"{@","@}",
			"\$cms::","\$bbs::","\$cate::","\$blog::","\$db::",
			"<!--#","#-->",
			);
			$e = array(
			"\r\nEOT;\r\necho ",";\r\nprint <<<EOT\r\n",
			"\$cms->","\$bbs->","\$cate->","\$blog->","\$db->",
			"\nEOT;\n","\nprint <<<EOT\n",
			);
			if(function_exists('str_ireplace')){
				$file_str = str_ireplace($s,$e,$file_str);
			}else{
				$file_str = str_replace($s,$e,$file_str);
			}
			$file_str = tplRarse($file_str);
			$file_str = str_replace("print <<<EOT\n\nEOT;",'',$file_str);
			$file_str = "<?php\r\nprint <<<EOT\r\n".$file_str."\r\nEOT;\n?>";
		}else{
			$s = array("{@","@}","\$cms::","\$bbs::","\$cate::","\$blog::","\$db::");
			$e = array("\r\nEOT;\r\n echo ",";\r\nprint <<<EOT\r\n","\$cms->","\$bbs->","\$cate->","\$blog->","\$db->");
			$file_str = str_replace($s,$e,$file_str);
		}
		writeover(D_P.$cache_name,$file_str);
	}
	return $cache_name;
}

/**
 * 解析CMS标签和Loop标签
 *
 * @author aileenguan
 * @param string $string
 * @return string
 */
function tplRarse($string)
{
	$replace = $condition = array();
	preg_match_all("/<cms(.+)\/>/",$string,$reg);
	$replace = $condition = array();
	foreach ($reg[1] as $id=>$val){
		$parameter = '';
		$condition[$id] = array();
		preg_match_all("/[[:blank:]]+?[a-zA-Z\-_]+=(['|\"]?).*?(\\1)/",$val,$match);
		foreach ($match[0] as $cmsReg){
			$pos = strpos($cmsReg,"=");
			$key = trim(substr($cmsReg,0,$pos));			
			$value = substr($cmsReg,$pos+1);
			if (preg_match("/^('|\")(.*?)(\\1)$/",$value,$newValue)) {
				$value = trim($newValue[2]); //去掉单引号和双引号
			}
			$condition[$id][$key] = $value;

			if($key!='type' && $key!='action'){
				$parameter.="$key:$value;";
			}
		}
		if (!$condition[$id]['type']) { //Type参数表明是要调用什么类，默认为cms类
			$condition[$id]['type']='cms';
		}
		if (!$condition[$id]['action']) { //Action参数表明要执行的类的方法
			throwError('tpl_noaction');
		}
		$replace[$id] = "\nEOT;\n\$array = \$".$condition[$id]['type']."->".$condition[$id]['action']."(";
		if ($condition[$id]['type'] == 'cms' && $condition[$id]['action'] == 'thread') { //内容列表
			$replace[$id] .= "\"".$parameter."\"";	
		}elseif ($condition[$id]['type'] == 'bbs' || $condition[$id]['type'] == 'blog') { //BBS排行
			$replace[$id] .= $condition[$id]['num'].','.$condition[$id]['order'].','.$condition[$id]['fid'];
		}elseif ($condition[$id]['type'] == 'cate'){ //栏目导航类
			if ($condition[$id]['action'] == 'menu') {
				$replace[$id] .= $condition[$id]['cid'];
			}
		}
		$replace[$id] .= ");\nprint <<<EOT";
		
	}
	/*
	foreach ($cms[1] as $id=>$val){ //循环每一个<CMS />
	$condition[$id] = array();
	$parameter = '';
	$paras = explode(" ",trim($val));
	foreach ($paras as $para){ //遍历每个<CMS />中的具体参数
	list($key,$value) = explode("=",$para);
	$key = trim($key); //去除首尾的空格
	$value = trim($value);
	if (preg_match("/^('|\")([^[:blank:]]*)(\\1)$/",$value,$reg)) {
	$value = $reg[2]; //去掉单引号和双引号
	}
	$condition[$id][$key] = $value;
	if($key!='type' && $key!='action'){
	$parameter.="$key:$value;";
	}
	}
	if (!$condition[$id]['type']) { //Type参数表明是要调用什么类，默认为cms类
	$condition[$id]['type']='cms';
	}
	if (!$condition[$id]['action']) { //Action参数表明要执行的类的方法
	throwError('tpl_noaction');
	}
	$replace[$id] = "EOT;\n\$array = \$".$condition[$id]['type']."->".$condition[$id]['action']."(\"";
	$replace[$id] .= $parameter."\");\nprint <<<EOT";
	}
	*/
	$string = str_replace($reg[0],$replace,$string);
	$string = str_replace("<loop>","\nEOT;\nforeach(\$array as \$key=>\$val){\nprint <<<EOT",$string);
	$string = str_replace("</loop>","\nEOT;\n}print <<<EOT\n",$string);
	return $string;
}
?>